package com.technology.education.ui.teacher.order_detail

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.SpKeys
import com.technology.education.data.model.DurationWithdrawModel
import com.technology.education.data.model.ExchangeGoodsModel
import com.technology.education.data.model.TeacherOrderDetailModel
import com.technology.education.ext.postEventBus
import com.technology.education.ext.sendTakeOrder
import com.technology.education.ext.toast
import com.technology.education.ext.videoCall
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

class OrderDetailViewModel : BaseViewModel() {
    var orderId = ""
    var studentId = ""
    val result = MutableLiveData<ResultState<TeacherOrderDetailModel>>()
    val takeResult = MutableLiveData<ExchangeGoodsModel>()
    val resultCharge = MutableLiveData<ResultState<DurationWithdrawModel>>()
    override fun getData() {
        super.getData()
        request({ apiService.teachOrderDetailTeacher(orderId) }, result)
    }

    /**
     * 接单
     */
    fun takeOrder() {
        request({ apiService.takeOrder(hashMapOf("id" to orderId)) }, success = {
            takeResult.postValue(it)
            sendTakeOrder(orderId, studentId)
        }, error = {
            toast(it.errorMsg)
        }, isShowDialog = true)
    }

    fun commitOrder() {
        request({ apiService.placeDepositOrder() }, resultCharge, isShowDialog = true)
    }

    /**
     * 开始辅导,这时候开始计时
     */
    fun startTeach() {
        request({ apiService.startOrder(hashMapOf("id" to orderId)) }, success = {
            postEventBus(SpKeys.CHANGE_ORDER_STATUS_START, orderId)
        }, error = {
            postEventBus(SpKeys.CHANGE_ORDER_STATUS_START, "")
            toast(it.errorMsg)
        })
    }

    /**
     * 结束视频辅导
     */
    fun finishOrder() {
        request(
            { apiService.endOrder(hashMapOf("id" to orderId)) },
            success = {
                postEventBus(SpKeys.CHANGE_ORDER_STATUS_WAIT_EVALUATE, orderId)
            },
            error = { toast(it.errorMsg) })
    }
}